    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cc.altius.dao.impl;

import cc.altius.dao.CustomerDao;
import cc.altius.domain.CustomUserDetails;
import cc.altius.domain.mapper.CustomerRowMapper;
import cc.altius.exception.CouldNotSaveToDataBaseException;
import cc.altius.model.*;
import cc.altius.utils.DateUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sql.DataSource;

import org.hibernate.*;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author sagar
 */
@Repository("CustomerDao")
public class CustomerDaoImpl implements CustomerDao {

    private SessionFactory sessionFactory;
    private HibernateTemplate hibernateTemplate;
    private DataSource dataSource;
    private JdbcTemplate jdbcTemplate;
    Session session;
    Query query;
    private static int RELATION_TYPE = 6;

    @Autowired
    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    @Autowired
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
        this.hibernateTemplate = new HibernateTemplate(sessionFactory);
    }

    @Override
    @Transactional
    public List<Map<String, Object>> searchCustomer(String mrNo, String phone, String customerName, String empId1) {
        session = this.sessionFactory.getCurrentSession();
        String s = " SELECT new map(c.customerId as customerId,"
                + " c.empId as empId, "
                + " c.customerName as customerName, "
                + " r.relationName as relation, "
                + " c.active as active, "
                + " c.eligible as eligible, "
                + " c.important as important, "
                + " c.phone1 as phone1,"
                + " c.lastModifiedDate as lastModifiedDate,"
                + " d.dispositionId as dispositionId,"
                + " c.followupDate as followupDate,"
                + " d.dispositionName as dispositionName)"
                + " from Customer c"
                + " left join c.disposition d "
                + " left join c.relation r ";
        if (!"".equals(mrNo)) {
            s += " WHERE c.mrNo='" + mrNo + "' ";
        } else if (!"".equals(phone)) {
            s += " WHERE c.phone1='" + phone + "' "
                    + "or "
                    + "c.phone2='" + phone + "' "
                    + " or "
                    + "c.phone3='" + phone + "' "
                    + " or "
                    + " c.phone4='" + phone + "' ";
        } else if (!"".equals(customerName)) {
            s += " WHERE c.customerName like '%" + customerName + "%'";
        } else {
            s += " WHERE c.empId='" + empId1 + "' ";
        }
        query = session.createQuery(s);
        //query.setParameter("userId", userId);
        List<Map<String, Object>> tmpCustomerList = query.list();
        return tmpCustomerList;
    }

    @Override
    @Transactional
    public List<PaymentType> getPaymentTypeList() {
        List<PaymentType> paymentType = this.hibernateTemplate.find("from PaymentType");
        return paymentType;
    }

    @Override
    @Transactional
    public Customer getCustomer(String phone, String mrNo, String customerName) {
        session = this.sessionFactory.getCurrentSession();
        String hql = "From Customer  where ";
        if (!"".equals(mrNo)) {
            hql += "mrNo='" + mrNo + "'";
        }
        if (!"".equals(customerName)) {
            hql += "customerName='" + customerName + "'";
        }
        if (!"".equals(phone)) {
            hql += "phone1='" + phone + "' "
                    + "or "
                    + "phone2='" + phone + "' "
                    + "or "
                    + "phone3='" + phone + "' "
                    + "or "
                    + "phone4='" + phone + "'";
        }
        query = session.createQuery(hql);

        Customer customer = (Customer) query.uniqueResult();
        System.out.println("customer" + customer);
        return customer;
    }

    @Override
    @Transactional
    public void updateCustomer(Customer customer, Date transFollowupDate, String transNotes, String transCheifComplaint, int callTypeId, int dispositionId, String area, String recordingId, int hospitalId, int appSpecialistId) {
        session = this.hibernateTemplate.getSessionFactory().getCurrentSession();

        try {
            int curUser = ((CustomUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUserId();
            Date curDate = DateUtils.getCurrentDateObject(DateUtils.IST);

            String hql = "Update "
                    + " Customer customer set"
                    + " customer.important=:important, "
                    + " customer.sms=:sms,"
                    + " customer.email=:email,"
                    + " customer.paymentTypeId.paymentTypeId=:paymentTypeId,"
                    + " customer.mrNo=:mrNo,"
                    + " customer.familyCode=:familyCode,"
                    + " customer.customerName=:customerName,"
                    + " customer.phone1=:phone1,"
                    + " customer.phone2=:phone2,"
                    + " customer.lastModifiedDate=:lastModifiedDate, "
                    + " customer.lastModifiedBy=:lastModifiedBy,"
                    + " customer.notes=:notes, "
                    + " customer.followupDate=:transFollowupDate,"
                    + " customer.age=:age, "
                    + " customer.gender=:gender, "
                    + " customer.active=:active,"
                    + " customer.eligible=:eligible,"
                    + " customer.dependence=:dependence,";

            if (!customer.getMaritalStatus().equals(null)) {
                hql += " customer.maritalStatus='" + customer.getMaritalStatus() + "',";
            }
            if (!customer.getCostCenter().equals(null)) {
                hql += " customer.costCenter='" + customer.getCostCenter() + "',";
            }
            if (customer.getZipCodeArea() != null) {
                hql += " ZIP_CODE='" + customer.getZipCodeArea().getZipCode() + "', ";
            }
            hql += " customer.officeLocation.officeId=:officeId,"
                    + " customer.dateOfJoining=:dateOfJoining,"
                    + " customer.relation.relationId=:relationId,"
                    + " customer.cheifComplaint=:cheifComplaint,"
                    + " customer.callType.callTypeId=:callTypeId,"
                    + " customer.disposition.dispositionId=:dispositionId,"
                    + " customer.address=:address,"
                    + " customer.road=:road,"
                    + " customer.landmark=:landmark,"
                    + " customer.doctor.doctorId=:doctorId,"
                    + " customer.emailId=:emailId,"
                    + " customer.bloodGroup=:bloodGroup,"
                    + " customer.status=:status,"
                    + " customer.healthSpringCenter.centerId=:centerId,"
                    + " customer.registrationStatus=:registrationStatus,"
                    + " customer.food=:food,"
                    + " customer.drug=:drug,"
                    + " customer.otherAllergies=:otherAllergies,"
                    + " customer.otherIllness=:otherIllness,"
                    + " customer.dateOfBirth=:dateOfBirth"
                    + " WHERE customer.customerId=:customerId";
            query = session.createQuery(hql);
            query.setParameter("important", customer.isImportant());
            query.setParameter("sms", customer.isSms());
            query.setParameter("email", customer.isEmail());
            query.setParameter("paymentTypeId", customer.getPaymentTypeId().getPaymentTypeId());
            query.setParameter("mrNo", customer.getMrNo());
            query.setParameter("familyCode", customer.getFamilyCode());
            query.setParameter("customerName", customer.getCustomerName());
            query.setParameter("phone1", customer.getPhone1());
            query.setParameter("phone2", customer.getPhone2());
            query.setParameter("lastModifiedDate", curDate);
            query.setParameter("lastModifiedBy", new User(curUser));
            query.setParameter("notes", transNotes);
            query.setParameter("transFollowupDate", customer.getFollowupDate());
            query.setParameter("dateOfBirth", customer.getDateOfBirth());
            query.setParameter("age", customer.getAge());
            query.setParameter("gender", customer.getGender());
            query.setParameter("active", customer.isActive());
            query.setParameter("eligible", customer.isEligible());
            query.setParameter("dependence", customer.getDependence());

            // query.setParameter("zipCode2", customer.getZipCodeArea().getZipCode());
            //query.setParameter("pincode", customer.getPincode());
//            if ("N/A".equals(area) || "".equals(area)) {
//                System.out.println("inside if()");
//                query.setParameter("zipCode2", null);
//
//            } else {
//                System.out.println("inside else");
//                query.setParameter("zipCode2", customer.getZipCodeArea().getZipCode());
//            }
            query.setParameter("officeId", customer.getOfficeLocation().getOfficeId());
            //System.out.println("in daoimpl :" + customer.getDateOfJoining());
            query.setParameter("dateOfJoining", customer.getDateOfJoining());
            query.setParameter("relationId", customer.getRelation().getRelationId());
            query.setParameter("cheifComplaint", transCheifComplaint);
            query.setParameter("callTypeId", callTypeId);
            query.setParameter("dispositionId", dispositionId);
            query.setParameter("address", customer.getAddress());
            query.setParameter("road", customer.getRoad());
            query.setParameter("landmark", customer.getLandmark());
//        if (customer.getDoctor().getDoctorId() == 0) {
//            System.out.println("if (customer.getDoctor().getDoctorId() == 0)");
//            customer.setDoctor(null);
//            query.setParameter("doctorId", customer.getDoctor().getDoctorId());
//        } else {
//            query.setParameter("doctorId", customer.getDoctor().getDoctorId());
//        }
            query.setParameter("emailId", customer.getEmailId());
            query.setParameter("bloodGroup", customer.getBloodGroup());
            query.setParameter("status", customer.getStatus());
            query.setParameter("centerId", customer.getHealthSpringCenter().getCenterId());
            query.setParameter("registrationStatus", customer.getRegistrationStatus());
//        query.setParameter("knownAllergy", customer.getKnownAllergy());
            query.setParameter("food", customer.getFood());
            query.setParameter("drug", customer.getDrug());
            query.setParameter("otherAllergies", customer.getOtherAllergies());
            query.setParameter("otherIllness", customer.getOtherIllness());
//      query.setParameter("knownIllness", customer.getKnownIllness());
            query.setParameter("customerId", customer.getCustomerId());
            //       query.executeUpdate();

            System.out.println("query " + query.getQueryString());

            TransCustomer transCustomer = new TransCustomer();
            transCustomer.setCustomerId(new Customer(customer.getCustomerId()));
            transCustomer.setLastModifiedDate(curDate);
            transCustomer.setLastModifiedBy(new User(curUser));
            transCustomer.setNotes(transNotes);
            transCustomer.setCallType(new CallType(callTypeId));
            transCustomer.setDisposition(new Disposition(dispositionId));
            transCustomer.setCheifComplaint(transCheifComplaint);
            transCustomer.setFollowupDate(customer.getFollowupDate());
            System.out.println("transCustomer.getFollowupDate" + transCustomer.getFollowupDate());
            transCustomer.setTransStatus(customer.getStatus());
            transCustomer.setRecordingId(recordingId);
            if (hospitalId != 0) {
                transCustomer.setHospital(new Hospital(hospitalId));
            } else {
                transCustomer.setHospital(null);
            }
            if (appSpecialistId != 0) {
                transCustomer.setAppSpecialist(new AppSpecialist(appSpecialistId));
            } else {
                transCustomer.setAppSpecialist(null);
            }
            if (customer.getDoctor().getDoctorId() == 0) {
                System.out.println("if (customer.getDoctor().getDoctorId() == 0)");
                customer.setDoctor(null);
                query.setParameter("doctorId", null);
                transCustomer.setDoctor(null);
            } else {
                query.setParameter("doctorId", customer.getDoctor().getDoctorId());
                transCustomer.setDoctor(customer.getDoctor());
            }
//        if (customer.getDoctor() == null) {
//            transCustomer.setDoctor(null);
//        } else {
//            transCustomer.setDoctor(customer.getDoctor());
//        }
            //getHibernateTemplate().bulkUpdate(query.getQueryString(), [params]);
            query.executeUpdate();
            System.out.println("after executeUpdate ");
            session.save(transCustomer);
            System.out.println("after save ");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    @Transactional
    public List<TransCustomer> getCustomerTransDetails(int customerId) {
        List<TransCustomer> l = null;
        System.out.println("customerId " + customerId);
        try {
            l = this.hibernateTemplate.find("select t FROM TransCustomer t WHERE t.customerId.customerId=" + customerId + " ORDER BY t.lastModifiedDate DESC");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        if (l.isEmpty()) {
            return null;
        } else {
            return l;
        }

        //session = this.hibernateTemplate.getSessionFactory().getCurrentSession();
//        query = session.createQuery("SELECT t FROM TransCustomer t WHERE t.customerId.customerId=:customerId ORDER BY t.lastModifiedDate DESC");
//        query.setParameter("customerId", customerId);
//        return query.list();
    }

    @Override
    @Transactional
    public List<Map<String, Object>> getFollowupList(int dispositionId) {
        int curUser = ((CustomUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUserId();
        String roleName = ((CustomUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getRole().getRoleId();
        //System.out.println("roleName :"+roleName);
        session = this.sessionFactory.getCurrentSession();
        String s = " SELECT new map(c.customerId as customerId,"
                + " c.customerName as customerName, "
                + " c.empId as empId,"
                + " c.mrNo as mrNo,"
                + " c.phone1 as phone1,"
                + " ct.callTypeName as callTypeName,"
                + " c.followupDate as followupDate,"
                + " c.status as status,"
                + " c.important as important,"
                + " c.lastModifiedDate as lastModifiedDate,"
                + " c.registrationStatus as registrationStatus,"
                + " d.dispositionId as dispositionId,"
                + " user.username as createdBy,"
                + " d.dispositionName as dispositionName)"
                + " from Customer c"
                + " left join c.disposition d "
                + " left join c.lastModifiedBy user "
                + " left join c.callType ct "
                + " WHERE c.followupDate is not null";

        if (dispositionId != 0) {
            s += " and d.dispositionId=" + dispositionId + " ";
        }
        s += "  ORDER by c.followupDate ASC";
        query = session.createQuery(s);
        //query.setParameter("userId", userId);
        List<Map<String, Object>> tmpCustomerList = query.list();
        return tmpCustomerList;
    }

    @Override
    @Transactional
    public List<Relation> getRelationList() {
        List<Relation> relation = this.hibernateTemplate.find("from Relation");
        return relation;
    }

    @Override
    @Transactional
    public List<CallType> getCallTypeList() {
        List<CallType> callType = this.hibernateTemplate.find("from CallType");
        return callType;
    }

    @Override
    @Transactional
    public List<Disposition> getDispositionList() {
        List<Disposition> dispositions = this.hibernateTemplate.find("from Disposition");
        return dispositions;
    }

//    @Override
//    @Transactional
//    public int addCustomer(Customer customer, String recordingId, int hospitalId, int appSpecialistId) {
//        try {
//            session = this.sessionFactory.getCurrentSession();
//            int curUser = ((CustomUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUserId();
//            Date curDate = DateUtils.getCurrentDateObject(DateUtils.IST);
//            customer.setLastModifiedBy(new User(curUser));
//            customer.setLastModifiedDate(curDate);
//            customer.setCreatedDate(curDate);
//            customer.setCreatedBy(new User(curUser));
//            String status = "Open";
//            customer.setStatus(status);
//            //session = this.sessionFactory.getCurrentSession();
//            TransCustomer transCustomer = new TransCustomer();
//
//            transCustomer.setLastModifiedBy(new User(curUser));
//            transCustomer.setLastModifiedDate(curDate);
//            transCustomer.setNotes(customer.getNotes());
//            transCustomer.setCallType(new CallType(customer.getCallType().getCallTypeId()));
//            transCustomer.setDisposition(new Disposition(customer.getDisposition().getDispositionId()));
//            transCustomer.setCheifComplaint(customer.getCheifComplaint());
//            transCustomer.setTransStatus(customer.getStatus());
//            transCustomer.setFollowupDate(customer.getFollowupDate());
//            transCustomer.setRecordingId(recordingId);
//            if (hospitalId != 0) {
//
//                transCustomer.setHospital(new Hospital(hospitalId));
//
//            } else {
//                transCustomer.setHospital(null);
//            }
//            if (appSpecialistId != 0) {
//                transCustomer.setAppSpecialist(new AppSpecialist(appSpecialistId));
//            } else {
//                transCustomer.setAppSpecialist(null);
//            }
//
//            if (customer.getDoctor().getDoctorId() == 0) {
//                customer.setDoctor(null);
//                transCustomer.setDoctor(null);
//            } else {
//
//                transCustomer.setDoctor(customer.getDoctor());
//            }
//
//            int custId = (Integer) this.hibernateTemplate.save(customer);
//
//            transCustomer.setCustomerId(new Customer(custId));
//
//            hibernateTemplate.save(transCustomer);
//            return custId;
//        } catch (Exception e) {
//            e.printStackTrace();
//            return 0;
//        }
//    }
    @Override
    @Transactional
    public Customer getCustomerDetails(int customerId) {
        session = this.sessionFactory.getCurrentSession();
        Criteria criteria = session.createCriteria(Customer.class);
        criteria.add(Restrictions.eq("customerId", customerId));
        return (Customer) criteria.uniqueResult();
    }

    @Override
    @Transactional
    public List<Illness> getIllnessList() {
        List<Illness> illnessList = this.hibernateTemplate.find("from Illness");
        return illnessList;
    }

    @Override
    @Transactional
    public int addIllness(int customerId, String selectedIllnessIds) {
        session = this.hibernateTemplate.getSessionFactory().getCurrentSession();
        if (!selectedIllnessIds.equals("")) {
            String arrselectedParameters[] = selectedIllnessIds.split(",");
            int intarray[] = new int[arrselectedParameters.length];
            for (int i = 0; i < arrselectedParameters.length; i++) {
                intarray[i] = Integer.parseInt(arrselectedParameters[i]);
                CustomerIllness customerIllness = new CustomerIllness();
                Illness illness = new Illness();
                illness.setIllnessId(intarray[i]);
                customerIllness.setIllness(illness);
                Customer customer = new Customer();
                customer.setCustomerId(customerId);
                customerIllness.setCustomer(customer);
                hibernateTemplate.save(customerIllness);
            }
        }
        return 0;
    }

    @Override
    @Transactional
    public List<Map<String, Object>> getIllnessListByCustomerId(int customerId) {
        System.out.println("customerId" + customerId);
        String sql = "SELECT  "
                + " customer_illness.`ILLNESS_ID`,illness.`ILLNESS_NAME`"
                + " FROM customer_illness LEFT JOIN illness"
                + " ON customer_illness.`ILLNESS_ID`=illness.`ILLNESS_ID`"
                + " WHERE customer_illness.`CUSTOMER_ID`='" + customerId + "'";
        System.out.println("sql=" + sql);
        List<Map<String, Object>> map;
        try {
            map = jdbcTemplate.queryForList(sql);
            System.out.println("map" + map.isEmpty());
            return map;
        } catch (Exception e) {
            System.out.println("" + e.getMessage());
            return null;

        }
    }

    @Override
    @Transactional
    public List<Map<String, Object>> getIllnessListForLeftTable(int customerId) {
        String sql = " SELECT "
                + " illness.`ILLNESS_NAME`,"
                + " illness.`ILLNESS_ID` "
                + " FROM illness LEFT JOIN customer_illness "
                + " ON customer_illness.`ILLNESS_ID`=illness.`ILLNESS_ID` "
                + " WHERE (customer_illness.`CUSTOMER_ID`!='" + customerId + "' OR customer_illness.`CUSTOMER_ID` IS NULL)";

        String sql1 = "SELECT illness.`ILLNESS_NAME`,illness.`ILLNESS_ID` AS iid,ccil.`ILLNESS_ID` AS cid "
                + " FROM illness "
                + " LEFT JOIN (SELECT customer_illness.`ILLNESS_ID`,customer_illness.`CUSTOMER_ID` FROM customer_illness "
                + " WHERE customer_illness.`CUSTOMER_ID`='" + customerId + "') AS ccil"
                + " ON illness.`ILLNESS_ID`=ccil.`ILLNESS_ID`"
                + " WHERE ccil.`ILLNESS_ID` IS NULL";
        List<Map<String, Object>> map;
        //try {
        map = jdbcTemplate.queryForList(sql1);
        //System.out.println("lenght of map" + map.size());
        return map;
        // } catch (Exception e) {
        //    System.out.println("" + e.getMessage());
        //     return null;

        // }
    }

    @Override
    @Transactional
    public void updateIllness(int customerId, String selectedIllnessIds) {
        session = this.hibernateTemplate.getSessionFactory().getCurrentSession();
        String hql = "Delete From CustomerIllness Where customer.customerId=:customerId ";
        query = session.createQuery(hql);
        query.setParameter("customerId", customerId);
        query.executeUpdate();
        if (!selectedIllnessIds.equals("")) {
            String arrselectedParameters[] = selectedIllnessIds.split(",");
            int intarray[] = new int[arrselectedParameters.length];

            for (int i = 0; i < arrselectedParameters.length; i++) {
                intarray[i] = Integer.parseInt(arrselectedParameters[i]);
                CustomerIllness ci = new CustomerIllness();
                Customer c = new Customer();
                c.setCustomerId(customerId);
                ci.setCustomer(c);
                Illness illness = new Illness();
                illness.setIllnessId(intarray[i]);
                ci.setIllness(illness);
                this.hibernateTemplate.save(ci);
            }
        }
    }

    @Override
    public List<Map<String, Object>> getCallLogReport(String startDate, String stopDate, int callTypeId, int dispositionId, int doctorId, int userId) {
        String sql = "CALL callLogReport(?,?,?,?,?,?)";
        return jdbcTemplate.queryForList(sql, startDate, stopDate, callTypeId, dispositionId, doctorId, userId);
    }

    @Override
    @Transactional
    public List<Disposition> getDispositionList(int callTypeId) {
        session = this.hibernateTemplate.getSessionFactory().getCurrentSession();
        String hql = "Select d From Disposition  d left join d.getCallType c Where c.callTypeId=" + callTypeId;
        query = session.createQuery(hql);
        return query.list();
    }

    //for sms type 9
    @Override
    @Transactional
    public Customer getCustomerListOnEmployeeId(int customerId) {
        //query using HIbernate
//        session = this.hibernateTemplate.getSessionFactory().getCurrentSession();
//        String hql = "from Customer as c left join Customer as pri Where c.empId="+empId;
//        query=session.createQuery(hql);
//        return query.list();

        //Query Using JdbcTemplate
        String sql = "SELECT"
                + " customer.`CUSTOMER_ID`,"
                + " customer.`CUSTOMER_NAME`,"
                + " relation.`RELATION_NAME`,"
                + " p.`CUSTOMER_NAME` AS pCustomerName,"
                + " p.`CUSTOMER_ID` AS pCustomerId,"
                + " p.`EMP_ID` AS pEmpId,"
                + " pRel.`RELATION_NAME` AS pRelationName   "
                + " FROM customer"
                + " LEFT JOIN relation ON customer.`RELATION_ID`=relation.`RELATION_ID` "
                + " LEFT JOIN customer AS p ON customer.`EMP_ID`=p.`EMP_ID` AND p.`RELATION_ID`=6"
                + " LEFT JOIN relation AS pRel ON p.`RELATION_ID`=pRel.`RELATION_ID`"
                + " WHERE customer.`CUSTOMER_ID`=?";
        Object params[] = new Object[]{customerId};
        Customer customer = this.jdbcTemplate.queryForObject(sql, params, new CustomerRowMapper());
        return customer;



    }

    @Override
    @Transactional
    public List<Map<String, Object>> getListOfTeleConsult(String date) {
        session = this.hibernateTemplate.getSessionFactory().getCurrentSession();
        String hql = "Select new map(c.customerName as customerName,c.phone1 as phone1)"
                + " from Customer c"
                + " left join c.callType ct"
                + " where ct.callTypeId=6 and"
                + " c.followupDate>='" + date + " 00:00:00' and c.followupDate<='" + date + " 23:23:59'";
        query = session.createQuery(hql);
        return query.list();
    }

    @Override
    @Transactional
    public void updateCustomerForOnlineRegistration(Customer customer) {
        session = this.hibernateTemplate.getSessionFactory().getCurrentSession();
        Boolean b = true;
        try {
            int curUser = ((CustomUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUserId();
            Date curDate = DateUtils.getCurrentDateObject(DateUtils.IST);

            String hql = "Update "
                    + " Customer customer set"
                    + " customer.important=:important, "
                    + " customer.sms=:sms,"
                    + " customer.email=:email,"
                    + " customer.paymentTypeId.paymentTypeId=:paymentTypeId,"
                    + " customer.mrNo=:mrNo,"
                    + " customer.familyCode=:familyCode,"
                    + " customer.customerName=:customerName,"
                    + " customer.phone1=:phone1,"
                    + " customer.phone2=:phone2,"
                    + " customer.lastModifiedDate=:lastModifiedDate, "
                    + " customer.lastModifiedBy=:lastModifiedBy,"
                    + " customer.age=:age, "
                    + " customer.gender=:gender, "
                    + " customer.active=:active,"
                    + " customer.eligible=:eligible,"
                    + " customer.dependence=:dependence,";

            if (!customer.getMaritalStatus().equals(null)) {
                hql += " customer.maritalStatus='" + customer.getMaritalStatus() + "',";
            }
            if (!customer.getCostCenter().equals(null)) {
                hql += " customer.costCenter='" + customer.getCostCenter() + "',";
            }
            if (customer.getZipCodeArea() != null) {
                hql += " ZIP_CODE='" + customer.getZipCodeArea().getZipCode() + "', ";
            }
            hql += " customer.officeLocation.officeId=:officeId,"
                    + " customer.dateOfJoining=:dateOfJoining,"
                    + " customer.relation.relationId=:relationId,"
                    + " customer.address=:address,"
                    + " customer.road=:road,"
                    + " customer.landmark=:landmark,"
                    + " customer.emailId=:emailId,"
                    + " customer.bloodGroup=:bloodGroup,"
                    + " customer.status=:status,"
                    + " customer.healthSpringCenter.centerId=:centerId,"
                    + " customer.registrationStatus=:registrationStatus,"
                    + " customer.food=:food,"
                    + " customer.drug=:drug,"
                    + " customer.otherAllergies=:otherAllergies,"
                    + " customer.otherIllness=:otherIllness,"
                    + " customer.dateOfBirth=:dateOfBirth"
                    + " WHERE customer.customerId=:customerId";
            query = session.createQuery(hql);
            query.setParameter("important", customer.isImportant());
            query.setParameter("sms", customer.isSms());
            query.setParameter("email", customer.isEmail());
            query.setParameter("paymentTypeId", customer.getPaymentTypeId().getPaymentTypeId());
            query.setParameter("mrNo", customer.getMrNo());
            query.setParameter("familyCode", customer.getFamilyCode());
            query.setParameter("customerName", customer.getCustomerName());
            query.setParameter("phone1", customer.getPhone1());
            query.setParameter("phone2", customer.getPhone2());
            query.setParameter("lastModifiedDate", curDate);
            query.setParameter("lastModifiedBy", new User(1));
            query.setParameter("age", customer.getAge());
            query.setParameter("gender", customer.getGender());
            query.setParameter("active", customer.isActive());
            query.setParameter("eligible", customer.isEligible());
            query.setParameter("dependence", customer.getDependence());
            query.setParameter("officeId", customer.getOfficeLocation().getOfficeId());
            query.setParameter("dateOfJoining", customer.getDateOfJoining());
            query.setParameter("relationId", customer.getRelation().getRelationId());
            query.setParameter("address", customer.getAddress());
            query.setParameter("road", customer.getRoad());
            query.setParameter("landmark", customer.getLandmark());
            query.setParameter("emailId", customer.getEmailId());
            query.setParameter("bloodGroup", customer.getBloodGroup());
            query.setParameter("status", customer.getStatus());
            query.setParameter("centerId", customer.getHealthSpringCenter().getCenterId());
            query.setParameter("registrationStatus", customer.getRegistrationStatus());
            query.setParameter("food", customer.getFood());
            query.setParameter("drug", customer.getDrug());
            query.setParameter("otherAllergies", customer.getOtherAllergies());
            query.setParameter("otherIllness", customer.getOtherIllness());
            query.setParameter("dateOfBirth", customer.getDateOfBirth());
            query.setParameter("customerId", customer.getCustomerId());

            query.executeUpdate();
            System.out.println("after save ");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    @Transactional
    public int addCustomer(Customer customer) {
        try {
            session = this.sessionFactory.getCurrentSession();
            int curUser = ((CustomUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUserId();
            Date curDate = DateUtils.getCurrentDateObject(DateUtils.IST);
            customer.setLastModifiedBy(new User(curUser));
            customer.setLastModifiedDate(curDate);
            customer.setCreatedDate(curDate);
            customer.setCreatedBy(new User(curUser));
            String status = "Open";
            customer.setStatus(status);
            int custId = (Integer) this.hibernateTemplate.save(customer);
            return custId;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public List<Event> getEventList(int corporateId) {
        try {
            List<Event> list = this.hibernateTemplate.find("from Event where corporate.paymentTypeId=" + corporateId + " and active=" + 1);
            if (list.size() == 15) {
                return list;
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<CorporateCustomer> getCorporateCustomerList(String customerName, int corporateId) {
        try {
            return this.hibernateTemplate.find("from CorporateCustomer where name like '" + customerName + "%' And corporate.paymentTypeId=" + corporateId);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    @Transactional
    public CorporateCustomerEvent getCorporateCustomerEventData(int coCustomerId) {
        try {
            session = this.sessionFactory.getCurrentSession();
            query = this.session.createQuery("From CorporateCustomerEvent Where corporateCustomer.corporateCustomerId=:corporateCustomerId");
            query.setParameter("corporateCustomerId", coCustomerId);
            return (CorporateCustomerEvent) query.uniqueResult();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    @Transactional
    public int editCorporateCustomerEventData(CorporateCustomerEvent coCustomerEvents) {
        try {
            this.hibernateTemplate.update(coCustomerEvents);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public List<Map<String, Object>> getCorporateCustomerExcelData(String customerName, int corporateId) {
        String sql = "CALL listCorporateCustomer('" + customerName + "'," + corporateId + ")";
        return this.jdbcTemplate.queryForList(sql);
    }

    @Override
    @Transactional
    public int saveCorporateCustomerData(List<CorporateCustomer> corporateCustommerList, List<CorporateCustomerEvent> corporateCustommerEventList) {

        session = sessionFactory.getCurrentSession();
        Transaction transaction = session.beginTransaction();
        int curUser = ((CustomUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUserId();
        Date curDate = DateUtils.getCurrentDateObject(DateUtils.IST);
        int line = 0;
        try {
            if (corporateCustommerList.size() == corporateCustommerEventList.size()) {
                for (int loop = 0; loop < corporateCustommerList.size(); loop++) {
                    CorporateCustomer corporateCustomer = corporateCustommerList.get(loop);
                    line += 1;
                    User user = new User();
                    user.setUserId(curUser);
                    corporateCustomer.setCreatedBy(user);
                    corporateCustomer.setCreatedDate(curDate);
                    corporateCustomer.setLastModifiedBy(user);
                    corporateCustomer.setLastModifiedDate(curDate);

//                    int id = (Integer) this.hibernateTemplate.save(corporateCustomer);
                    int id = (Integer) this.session.save(corporateCustomer);

                    corporateCustomer.setCorporateCustomerId(id);

                    CorporateCustomerEvent corporateCustomerEvent = corporateCustommerEventList.get(loop);
                    corporateCustomerEvent.setCorporateCustomer(corporateCustomer);

                    if (corporateCustomerEvent.getScheduleEvent1() != null) {
                        corporateCustomerEvent.setLastModifiedBy1(user);
                        corporateCustomerEvent.setLastModifiedDate1(curDate);
                    }
                    if (corporateCustomerEvent.getScheduleEvent2() != null) {
                        corporateCustomerEvent.setLastModifiedBy2(user);
                        corporateCustomerEvent.setLastModifiedDate2(curDate);
                    }
                    if (corporateCustomerEvent.getScheduleEvent3() != null) {
                        corporateCustomerEvent.setLastModifiedBy3(user);
                        corporateCustomerEvent.setLastModifiedDate3(curDate);
                    }
                    if (corporateCustomerEvent.getScheduleEvent4() != null) {
                        corporateCustomerEvent.setLastModifiedBy4(user);
                        corporateCustomerEvent.setLastModifiedDate4(curDate);
                    }
                    if (corporateCustomerEvent.getScheduleEvent5() != null) {
                        corporateCustomerEvent.setLastModifiedBy5(user);
                        corporateCustomerEvent.setLastModifiedDate5(curDate);
                    }
                    if (corporateCustomerEvent.getScheduleEvent6() != null) {
                        corporateCustomerEvent.setLastModifiedBy6(user);
                        corporateCustomerEvent.setLastModifiedDate6(curDate);
                    }
                    if (corporateCustomerEvent.getScheduleEvent7() != null) {
                        corporateCustomerEvent.setLastModifiedBy7(user);
                        corporateCustomerEvent.setLastModifiedDate7(curDate);
                    }
                    if (corporateCustomerEvent.getScheduleEvent8() != null) {
                        corporateCustomerEvent.setLastModifiedBy8(user);
                        corporateCustomerEvent.setLastModifiedDate8(curDate);
                    }
                    if (corporateCustomerEvent.getScheduleEvent9() != null) {
                        corporateCustomerEvent.setLastModifiedBy9(user);
                        corporateCustomerEvent.setLastModifiedDate9(curDate);
                    }
                    if (corporateCustomerEvent.getScheduleEvent10() != null) {
                        corporateCustomerEvent.setLastModifiedBy10(user);
                        corporateCustomerEvent.setLastModifiedDate10(curDate);
                    }
                    if (corporateCustomerEvent.getScheduleEvent11() != null) {
                        corporateCustomerEvent.setLastModifiedBy11(user);
                        corporateCustomerEvent.setLastModifiedDate11(curDate);
                    }
                    if (corporateCustomerEvent.getScheduleEvent12() != null) {
                        corporateCustomerEvent.setLastModifiedBy12(user);
                        corporateCustomerEvent.setLastModifiedDate12(curDate);
                    }
                    if (corporateCustomerEvent.getScheduleEvent13() != null) {
                        corporateCustomerEvent.setLastModifiedBy13(user);
                        corporateCustomerEvent.setLastModifiedDate13(curDate);
                    }
                    if (corporateCustomerEvent.getScheduleEvent14() != null) {
                        corporateCustomerEvent.setLastModifiedBy14(user);
                        corporateCustomerEvent.setLastModifiedDate14(curDate);
                    }
                    if (corporateCustomerEvent.getScheduleEvent15() != null) {
                        corporateCustomerEvent.setLastModifiedBy15(user);
                        corporateCustomerEvent.setLastModifiedDate15(curDate);
                    }

                    /*
                     * //event1 Event event1 = new Event();
                     * event1.setEventId(1);
                     * corporateCustomerEvent.setEvent1(event1);
                     *
                     * //event2 Event event2 = new Event();
                     * event2.setEventId(2);
                     * corporateCustomerEvent.setEvent2(event2);
                     *
                     * //event3 Event event3 = new Event();
                     * event3.setEventId(3);
                     * corporateCustomerEvent.setEvent3(event3);
                     *
                     * //event4 Event event4 = new Event();
                     * event4.setEventId(4);
                     * corporateCustomerEvent.setEvent4(event4);
                     *
                     * //event5 Event event5 = new Event();
                     * event5.setEventId(5);
                     * corporateCustomerEvent.setEvent5(event5);
                     *
                     * //event6 Event event6 = new Event();
                     * event6.setEventId(6);
                     * corporateCustomerEvent.setEvent6(event6);
                     *
                     * //event7 Event event7 = new Event();
                     * event7.setEventId(7);
                     * corporateCustomerEvent.setEvent7(event7);
                     *
                     * //event8 Event event8 = new Event();
                     * event8.setEventId(8);
                     * corporateCustomerEvent.setEvent8(event8);
                     *
                     * //event9 Event event9 = new Event();
                     * event9.setEventId(9);
                     * corporateCustomerEvent.setEvent9(event9);
                     *
                     * //event10 Event event10 = new Event();
                     * event10.setEventId(10);
                     * corporateCustomerEvent.setEvent10(event10);
                     *
                     * //event11 Event event11 = new Event();
                     * event11.setEventId(11);
                     * corporateCustomerEvent.setEvent11(event11);
                     *
                     * //event12 Event event12 = new Event();
                     * event12.setEventId(12);
                     * corporateCustomerEvent.setEvent12(event12);
                     *
                     * //event13 Event event13 = new Event();
                     * event13.setEventId(13);
                     * corporateCustomerEvent.setEvent13(event13);
                     *
                     * //event14 Event event14 = new Event();
                     * event14.setEventId(14);
                     * corporateCustomerEvent.setEvent14(event14);
                     *
                     * //event15 Event event15 = new Event();
                     * event15.setEventId(15);
                     * corporateCustomerEvent.setEvent15(event15);
                     *
                     */
//                    int result = (Integer) this.hibernateTemplate.save(corporateCustomerEvent);
                    int result = (Integer) this.session.save(corporateCustomerEvent);

                }
                transaction.commit();
            } else {
                return line;
            }

        } catch (Exception e) {
            e.printStackTrace();
            transaction.rollback();
            return 0;
        }
        return line;
    }

    @Override
    @Transactional
    public boolean updateEvent(int corporateId, int eventId, String eventDate, int eventStatus) {
        CorporateCustomerEvent event = this.getCorporateCustomerEventData(corporateId);
        int serialId = 0;
        int curUser = ((CustomUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUserId();
        Date curDate = DateUtils.getCurrentDateObject(DateUtils.IST);
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yy");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            if (event.getEvent1().getEventId() == eventId) {
                serialId = 1;
                this.jdbcTemplate.update("UPDATE corporate_customer_event cce SET cce.`SCHEDULE_EVENT_1`='" + sdf2.format(sdf.parse(eventDate)) + "', cce.`STATUS_1`=" + eventStatus + " ,cce.`LAST_MODIFIED_BY1`=" + curUser + ", cce.`LAST_MODIFIED_DATE1`='" + sdf2.format(curDate) + "' WHERE cce.`CORPORATE_CUSTOMER_ID`=" + corporateId);
                return true;
            }
            if (event.getEvent2().getEventId() == eventId) {
                serialId = 2;
                this.jdbcTemplate.update("UPDATE corporate_customer_event cce SET cce.`SCHEDULE_EVENT_2`='" + sdf2.format(sdf.parse(eventDate)) + "', cce.`STATUS_2`=" + eventStatus + " ,cce.`LAST_MODIFIED_BY2`=" + curUser + ", cce.`LAST_MODIFIED_DATE2`='" + sdf2.format(curDate) + "' WHERE cce.`CORPORATE_CUSTOMER_ID`=" + corporateId);
                return true;
            }
            if (event.getEvent3().getEventId() == eventId) {
                serialId = 3;
                this.jdbcTemplate.update("UPDATE corporate_customer_event cce SET cce.`SCHEDULE_EVENT_3`='" + sdf2.format(sdf.parse(eventDate)) + "', cce.`STATUS_3`=" + eventStatus + " ,cce.`LAST_MODIFIED_BY3`=" + curUser + ", cce.`LAST_MODIFIED_DATE3`='" + sdf2.format(curDate) + "' WHERE cce.`CORPORATE_CUSTOMER_ID`=" + corporateId);
                return true;
            }
            if (event.getEvent4().getEventId() == eventId) {
                serialId = 4;
                this.jdbcTemplate.update("UPDATE corporate_customer_event cce SET cce.`SCHEDULE_EVENT_4`='" + sdf2.format(sdf.parse(eventDate)) + "', cce.`STATUS_4`=" + eventStatus + " ,cce.`LAST_MODIFIED_BY4`=" + curUser + ", cce.`LAST_MODIFIED_DATE4`='" + sdf2.format(curDate) + "' WHERE cce.`CORPORATE_CUSTOMER_ID`=" + corporateId);
                return true;
            }
            if (event.getEvent5().getEventId() == eventId) {
                serialId = 5;
                this.jdbcTemplate.update("UPDATE corporate_customer_event cce SET cce.`SCHEDULE_EVENT_5`='" + sdf2.format(sdf.parse(eventDate)) + "', cce.`STATUS_5`=" + eventStatus + " ,cce.`LAST_MODIFIED_BY5`=" + curUser + ", cce.`LAST_MODIFIED_DATE5`='" + sdf2.format(curDate) + "' WHERE cce.`CORPORATE_CUSTOMER_ID`=" + corporateId);
                return true;
            }
            if (event.getEvent6().getEventId() == eventId) {
                serialId = 6;
                this.jdbcTemplate.update("UPDATE corporate_customer_event cce SET cce.`SCHEDULE_EVENT_6`='" + sdf2.format(sdf.parse(eventDate)) + "', cce.`STATUS_6`=" + eventStatus + " ,cce.`LAST_MODIFIED_BY6`=" + curUser + ", cce.`LAST_MODIFIED_DATE6`='" + sdf2.format(curDate) + "' WHERE cce.`CORPORATE_CUSTOMER_ID`=" + corporateId);
                return true;
            }
            if (event.getEvent7().getEventId() == eventId) {
                serialId = 7;
                this.jdbcTemplate.update("UPDATE corporate_customer_event cce SET cce.`SCHEDULE_EVENT_7`='" + sdf2.format(sdf.parse(eventDate)) + "', cce.`STATUS_7`=" + eventStatus + " ,cce.`LAST_MODIFIED_BY7`=" + curUser + ", cce.`LAST_MODIFIED_DATE7`='" + sdf2.format(curDate) + "' WHERE cce.`CORPORATE_CUSTOMER_ID`=" + corporateId);
                return true;
            }
            if (event.getEvent8().getEventId() == eventId) {
                serialId = 8;
                this.jdbcTemplate.update("UPDATE corporate_customer_event cce SET cce.`SCHEDULE_EVENT_8`='" + sdf2.format(sdf.parse(eventDate)) + "', cce.`STATUS_8`=" + eventStatus + " ,cce.`LAST_MODIFIED_BY8`=" + curUser + ", cce.`LAST_MODIFIED_DATE8`='" + sdf2.format(curDate) + "' WHERE cce.`CORPORATE_CUSTOMER_ID`=" + corporateId);
                return true;
            }
            if (event.getEvent9().getEventId() == eventId) {
                serialId = 9;
                this.jdbcTemplate.update("UPDATE corporate_customer_event cce SET cce.`SCHEDULE_EVENT_9`='" + sdf2.format(sdf.parse(eventDate)) + "', cce.`STATUS_9`=" + eventStatus + " ,cce.`LAST_MODIFIED_BY9`=" + curUser + ", cce.`LAST_MODIFIED_DATE9`='" + sdf2.format(curDate) + "' WHERE cce.`CORPORATE_CUSTOMER_ID`=" + corporateId);
                return true;
            }
            if (event.getEvent10().getEventId() == eventId) {
                serialId = 10;
                this.jdbcTemplate.update("UPDATE corporate_customer_event cce SET cce.`SCHEDULE_EVENT_10`='" + sdf2.format(sdf.parse(eventDate)) + "', cce.`STATUS_10`=" + eventStatus + " ,cce.`LAST_MODIFIED_BY10`=" + curUser + ", cce.`LAST_MODIFIED_DATE10`='" + sdf2.format(curDate) + "' WHERE cce.`CORPORATE_CUSTOMER_ID`=" + corporateId);
                return true;
            }
            if (event.getEvent11().getEventId() == eventId) {
                serialId = 11;
                this.jdbcTemplate.update("UPDATE corporate_customer_event cce SET cce.`SCHEDULE_EVENT_11`='" + sdf2.format(sdf.parse(eventDate)) + "', cce.`STATUS_11`=" + eventStatus + " ,cce.`LAST_MODIFIED_BY11`=" + curUser + ", cce.`LAST_MODIFIED_DATE11`='" + sdf2.format(curDate) + "' WHERE cce.`CORPORATE_CUSTOMER_ID`=" + corporateId);
                return true;
            }
            if (event.getEvent12().getEventId() == eventId) {
                serialId = 12;
                this.jdbcTemplate.update("UPDATE corporate_customer_event cce SET cce.`SCHEDULE_EVENT_12`='" + sdf2.format(sdf.parse(eventDate)) + "', cce.`STATUS_12`=" + eventStatus + " ,cce.`LAST_MODIFIED_BY12`=" + curUser + ", cce.`LAST_MODIFIED_DATE12`='" + sdf2.format(curDate) + "' WHERE cce.`CORPORATE_CUSTOMER_ID`=" + corporateId);
                return true;
            }
            if (event.getEvent13().getEventId() == eventId) {
                serialId = 13;
                this.jdbcTemplate.update("UPDATE corporate_customer_event cce SET cce.`SCHEDULE_EVENT_13`='" + sdf2.format(sdf.parse(eventDate)) + "', cce.`STATUS_13`=" + eventStatus + " ,cce.`LAST_MODIFIED_BY13`=" + curUser + ", cce.`LAST_MODIFIED_DATE13`='" + sdf2.format(curDate) + "' WHERE cce.`CORPORATE_CUSTOMER_ID`=" + corporateId);
                return true;
            }
            if (event.getEvent14().getEventId() == eventId) {
                serialId = 14;
                this.jdbcTemplate.update("UPDATE corporate_customer_event cce SET cce.`SCHEDULE_EVENT_14`='" + sdf2.format(sdf.parse(eventDate)) + "', cce.`STATUS_14`=" + eventStatus + " ,cce.`LAST_MODIFIED_BY14`=" + curUser + ", cce.`LAST_MODIFIED_DATE14`='" + sdf2.format(curDate) + "' WHERE cce.`CORPORATE_CUSTOMER_ID`=" + corporateId);
                return true;
            }
            if (event.getEvent15().getEventId() == eventId) {
                serialId = 15;
                this.jdbcTemplate.update("UPDATE corporate_customer_event cce SET cce.`SCHEDULE_EVENT_15`='" + sdf2.format(sdf.parse(eventDate)) + "', cce.`STATUS_15`=" + eventStatus + " ,cce.`LAST_MODIFIED_BY15`=" + curUser + ", cce.`LAST_MODIFIED_DATE15`='" + sdf2.format(curDate) + "' WHERE cce.`CORPORATE_CUSTOMER_ID`=" + corporateId);
                return true;
            }
        } catch (ParseException ex) {
            Logger.getLogger(CustomerDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        return false;
    }

    @Override
    public PaymentType getCorporateById(int corporateId) {
        return this.hibernateTemplate.get(PaymentType.class, corporateId);
    }

    @Override
    public boolean editEvent(Event event) {
        try {
            int curUser = ((CustomUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUserId();
            Date curDate = DateUtils.getCurrentDateObject(DateUtils.IST);
            event.setLastModifiedDate(curDate);
            User user = new User();
            user.setUserId(curUser);
            event.setLastModifiedBy(user);
            this.hibernateTemplate.update(event);
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

    @Override
    @Transactional
    public int addCorporate(PaymentType corporate) {
        session = sessionFactory.getCurrentSession();
        Transaction transaction = session.beginTransaction();
        int curUser = ((CustomUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUserId();
        Date curDate = DateUtils.getCurrentDateObject(DateUtils.IST);
        try {
            User user = new User();
            user.setUserId(curUser);
            corporate.setCreatedBy(user);
            corporate.setLastModifiedBy(user);
            corporate.setCreatedDate(curDate);
            corporate.setLastModifiedDate(curDate);
//            return (Integer) this.hibernateTemplate.save(corporate);
            this.session.save(corporate);
            for (int i = 0; i < 15; i++) {
                Event event = new Event();
                event.setCorporate(corporate);
                event.setEventDescription("Coming Soon...");
                event.setActive(1);
                event.setLastModifiedBy(user);
                event.setLastModifiedDate(curDate);
                this.session.save(event);
            }
            transaction.commit();
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            transaction.rollback();
            return 0;
        }
    }

    @Override
    @Transactional
    public int editCorporate(PaymentType corporate) {
        int curUser = ((CustomUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUserId();
        Date curDate = DateUtils.getCurrentDateObject(DateUtils.IST);
        try {
            User user = new User();
            user.setUserId(curUser);
            corporate.setCreatedBy(user);
            corporate.setLastModifiedBy(user);
            corporate.setCreatedDate(curDate);
            corporate.setLastModifiedDate(curDate);
            this.hibernateTemplate.update(corporate);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    @Transactional
    public int addCorporateCustomer(CorporateCustomer corporateCustomer, CorporateCustomerEvent customerEvent) {
        session = this.sessionFactory.getCurrentSession();
        Transaction transaction = session.beginTransaction();
        int curUser = ((CustomUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUserId();
        Date curDate = DateUtils.getCurrentDateObject(DateUtils.IST);
        try {
            User user = new User();
            user.setUserId(curUser);
            corporateCustomer.setCreatedBy(user);
            corporateCustomer.setLastModifiedBy(user);
            corporateCustomer.setCreatedDate(curDate);
            corporateCustomer.setLastModifiedDate(curDate);
            int Id = (Integer) this.session.save(corporateCustomer);

            corporateCustomer.setCorporateCustomerId(Id);
            customerEvent.setCorporateCustomer(corporateCustomer);

            Id = (Integer) this.session.save(customerEvent);
            transaction.commit();
            return Id;
        } catch (Exception e) {
            e.printStackTrace();
            transaction.rollback();
            throw new CouldNotSaveToDataBaseException(e);
        }

    }

    @Override
    public CorporateCustomer getCorporateCustomerById(int corporateCustomerId) {
        try {
            return this.hibernateTemplate.get(CorporateCustomer.class, corporateCustomerId);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    @Transactional
    public int editCorporateCustomer(CorporateCustomer corporateCustomer) {
        int curUser = ((CustomUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUserId();
        Date curDate = DateUtils.getCurrentDateObject(DateUtils.IST);
        try {
            User user = new User();
            user.setUserId(curUser);
            corporateCustomer.setLastModifiedBy(user);
            corporateCustomer.setLastModifiedDate(curDate);
            this.hibernateTemplate.update(corporateCustomer);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public List<EventStatus> getEventStatusList() {
        return this.hibernateTemplate.find("From EventStatus");
    }
}
